package com.androidx.p013lv.base.utils;

import android.util.Base64;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/* renamed from: com.androidx.lv.base.utils.Base64Utils */
public class Base64Utils {
    private static int CACHE_SIZE = 1024;

    public static byte[] decode2byte(String str) {
        return Base64.decode(str, 0);
    }

    public static String decode2String(String str) {
        return new String(Base64.decode(str, 0));
    }

    public static String encode(String str) {
        return new String(Base64.encode(str.getBytes(), 0));
    }

    public static String encode(byte[] bArr) {
        return new String(Base64.encode(bArr, 0));
    }

    public static String encodeFile(String str) throws Exception {
        return encode(fileToByte(str));
    }

    public static void decodeToFile(String str, String str2) throws Exception {
        byteArrayToFile(decode2byte(str2), str);
    }

    public static byte[] fileToByte(String str) throws Exception {
        byte[] bArr = new byte[0];
        File file = new File(str);
        if (!file.exists()) {
            return bArr;
        }
        FileInputStream fileInputStream = new FileInputStream(file);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(2048);
        writeStream(fileInputStream, byteArrayOutputStream);
        return byteArrayOutputStream.toByteArray();
    }

    private static void writeStream(InputStream inputStream, OutputStream outputStream) throws IOException {
        byte[] bArr = new byte[CACHE_SIZE];
        while (true) {
            int read = inputStream.read(bArr);
            if (read != -1) {
                outputStream.write(bArr, 0, read);
                outputStream.flush();
            } else {
                outputStream.close();
                inputStream.close();
                return;
            }
        }
    }

    public static void byteArrayToFile(byte[] bArr, String str) throws Exception {
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bArr);
        File file = new File(str);
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        file.createNewFile();
        writeStream(byteArrayInputStream, new FileOutputStream(file));
    }
}
